package com.llkj.qingnianguoxue.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.bean.SignCalendarBean;
import com.llkj.qingnianguoxue.util.DisplayUtil;
import com.llkj.qingnianguoxue.util.StringUtils;

import java.util.List;

/**
 * 签到日历
 */
public class SignCalendarAdapter extends BaseAdapter {
    private Context mContext;
    private List<SignCalendarBean.Calendar> dateData;
    private LayoutInflater mInflater;

    public SignCalendarAdapter(Context context, List<SignCalendarBean.Calendar> data) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        dateData = data;
    }

    public void refreshData(List<SignCalendarBean.Calendar> data){
        dateData.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return dateData !=null ? dateData.size():0;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView==null){
            convertView = mInflater.inflate(R.layout.item_sign_calendar,null);
            holder = new ViewHolder();
            holder.tvDay = (TextView) convertView.findViewById(R.id.day);
            holder.tvSignIn = (TextView) convertView.findViewById(R.id.sign);
            convertView.setTag(holder);

            convertView.findViewById(R.id.flItem).getLayoutParams().height = (DisplayUtil.getDensityWdith(mContext)-DisplayUtil.dip2px(mContext,26))/7;
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        SignCalendarBean.Calendar calendar = dateData.get(position);
        if(!StringUtils.isEmpty(calendar.getDay())){
            holder.tvDay.setText(calendar.getDay());
        }else{
            holder.tvDay.setText("");
        }

        if(!StringUtils.isEmpty(calendar.getDay()) && "1".equals(calendar.getIsSign())){
            holder.tvSignIn.setVisibility(View.VISIBLE);
        }else{
            holder.tvSignIn.setVisibility(View.INVISIBLE);
        }
        return convertView;
    }

    class ViewHolder{
        TextView tvDay;
        TextView tvSignIn;
    }
}
